<template>
  <div>
    <div class="tag-container">
      <div class="tag-input">
        <span>x轴</span>
        <span class="tag" v-for="(tag, index) in xAxisTags" :key="index">
          {{ tag }}
          <button @click="removeTag('x', index)" v-if="xAxisTags.length > 1">x</button>
        </span>
        <button @click="toggleDropdown('x')">+</button>
        <div v-if="axisDropdowns.x" class="dropdown">
          <ul>
            <li v-for="(key, index) of Object.keys(propertyData)" :key="key" @click="addTag('x', key)">
              {{ key }}
              <div class="divider" v-if="index !== Object.keys(propertyData).length - 1"></div>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="tag-container">
      <div class="tag-input">
        <span>y轴</span>
        <span class="tag" v-for="(tag, index) in yAxisTags" :key="index">
          {{ tag }}
          <button @click="removeTag('y', index)" v-if="yAxisTags.length > 1">x</button>
        </span>
        <button @click="toggleDropdown('y')">+</button>
        <div v-if="axisDropdowns.y" class="dropdown">
          <ul>
            <li v-for="(key, index) of Object.keys(propertyData)" :key="key" @click="addTag('y', key)">
              {{ key }}
              <div class="divider" v-if="index !== Object.keys(propertyData).length - 1"></div>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    propertyData: Object
  },
  data() {
    return {
      xAxisTags: [],
      yAxisTags: [],
      axisDropdowns: { x: false, y: false }
    };
  },
  methods: {
    addTag(axis, key) {
      if (key) {
        if (axis === 'x') {
          this.xAxisTags.push(key);
          this.$emit("selected-xAxis-element", this.xAxisTags);
        } else {
          this.yAxisTags.push(key);
          this.$emit("selected-yAxis-element", this.yAxisTags);
        }
        this.axisDropdowns[axis] = false;
      }
    },
    removeTag(axis, index) {
      if (axis === 'x') {
        this.xAxisTags.splice(index, 1);
        console.log(this.xAxisTags)
        this.$emit("selected-xAxis-element", this.xAxisTags);
      } else {
        this.yAxisTags.splice(index, 1);
        this.$emit("selected-yAxis-element", this.yAxisTags);
        console.log(this.yAxisTags)
      }
    },
    toggleDropdown(axis) {
      this.axisDropdowns[axis] = !this.axisDropdowns[axis];
    }
  }
};
</script>

<style scoped>
.tag-container {
  display: flex;
  padding: 10px;
  align-items: center;
  position: relative; /* 添加相对定位 */
}
.tag-input {
  position: relative;
}
.tag {
  margin-right: 5px;
  margin-left: 5px;
  padding: 3px 6px;
  border-radius: 4px;
  background-color: #eee;
}
.tag button {
  cursor: pointer;
  font-size: 12px;
  padding: 0 3px;
  margin-left: 5px;
}
.dropdown {
  position: absolute; /* 使用绝对定位 */
  top: 100%;
  right: 0; /* 保持在加号按钮的右侧 */
  z-index: 1;
  background-color: white;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 5px;
  min-width: 150px;
}
.dropdown ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
.dropdown li {
  padding: 5px;
  cursor: pointer;
}
.dropdown li:hover {
  background-color: #f5f5f5;
}
.divider {
  height: 1px;
  background-color: #ccc;
  margin: 5px 0;
}
</style>